package online.inote.dic;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import online.inote.dic.common.constants.System.App;
import online.inote.dic.common.utils.IPUtils;
import online.inote.dic.common.utils.spring.PropertyFactory;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @description: 字典服务启动类
 * @author XQF.Sui
 * @created 2019年6月12日 下午10:14:19
 * @version 1.0
 */

@SpringBootApplication
@EnableTransactionManagement
@MapperScan(basePackages = "online.inote.dic.repository.mapper.*")
public class DicApplication implements WebMvcConfigurer, CommandLineRunner {

	private Logger logger = LoggerFactory.getLogger(DicApplication.class);
	
	public static void main(String[] args) {
		SpringApplication.run(DicApplication.class, args);
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		configureFastJsonHttpMessageConverter(converters);
		
	}
	
	private void configureFastJsonHttpMessageConverter(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		FastJsonConfig config = new FastJsonConfig();
		config.setSerializerFeatures(SerializerFeature.PrettyFormat);
		converter.setFastJsonConfig(config);

		List<MediaType> supportedMediaTypes = new ArrayList<>();

		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);

		converter.setSupportedMediaTypes(supportedMediaTypes);
		
		converters.add(converter);
	}
	
	@Override
	public void run(String... args) throws Exception {
		logger.info("========================================================================");
		logger.info("dic服务启动完成！");
		logger.info("dic运行环境:[ {} ]", PropertyFactory.getProperty(App.ACTIVE_KEY));
		logger.info("dic运行地址:[ {} ]", IPUtils.getLocalhostAddress());
		logger.info("dic运行端口:[ {} ]", PropertyFactory.getProperty(App.PORT_KEY));
		logger.info("dic运行参数:{}", JSON.toJSONString(args));
		logger.info("========================================================================");
	}
}